package ptij.vocoder.baseModel.closed_loop;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.BooleanToken;
import ptolemy.data.type.*;
import ptolemy.data.expr.Parameter;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// ImpResp
/** 

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class ImpResp extends TypedAtomicActor {

    public ImpResp(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        Aq = new TypedIOPort(this, "Aq", true, false);
        Aq.setTypeEquals(BaseType.OBJECT);
        
        ap1 = new TypedIOPort(this, "ap1", true, false);
        ap1.setTypeEquals(BaseType.OBJECT);
        
        ap2 = new TypedIOPort(this, "ap2", true, false);
        ap2.setTypeEquals(BaseType.OBJECT);

        txdtx_ctrl = new TypedIOPort(this, "txdtx_ctrl", true, false);
        txdtx_ctrl.setTypeEquals(BaseType.OBJECT);

        reset = new TypedIOPort(this, "reset", true, false);
        reset.setTypeEquals(BaseType.BOOLEAN);

        h1 = new TypedIOPort(this, "h1", false, true);
        h1.setTypeEquals(BaseType.OBJECT);
        
        update = new Parameter(this, "update", new BooleanToken(true));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort Aq;
    public TypedIOPort ap1;
    public TypedIOPort ap2;
    public TypedIOPort txdtx_ctrl;
    public TypedIOPort reset;

    // Output Ports
    public TypedIOPort h1;
    
    public Parameter update;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        _init();
    }

    public void fire() throws IllegalActionException {
        if(reset.hasToken(0)) {
            reset.get(0);
            _init();
        }
        
        ObjectToken obj = (ObjectToken) txdtx_ctrl.get(0);
        Short txdtx = (Short) obj.getValue();
        if((txdtx.shortValue() & Cnst.TX_SP_FLAG) != 0) {
            obj = (ObjectToken) Aq.get(0);
            Short [] arg1 = (Short []) obj.getValue();
            
            obj = (ObjectToken) ap1.get(0);
            Short [] arg2 = (Short []) obj.getValue();
            
            for(int i = 0;i < arg2.length;i++) {
                _zero[i] = arg2[i];
            }
            Short [] resp = _syn_filt(arg1, arg2);
            
            obj = (ObjectToken) ap2.get(0);
            arg2 = (Short []) obj.getValue();
            
            resp = _syn_filt(resp, arg2);
            
            h1.broadcast(new ObjectToken(resp));
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    private Short [] _syn_filt(Short [] a, Short [] x) {
        
        
        return null;
    }
    
    private void _init() throws IllegalActionException {
        _zero = new Short[Cnst.L_SUBFR];
        
        BooleanToken bt = (BooleanToken) update.getToken();
        _update = bt.booleanValue();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private Short [] _zero;
    private boolean _update;
}
